//
//  TimeSelectView.m
//  EducationAdminClient
//
//  Created by kwsdzjx on 16/1/15.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "TimeSelectView.h"
#import "Masonry.h"
#import "EducationAdminClient_URL.h"

@implementation TimeSelectView
{
    NSMutableArray      *yeaArr;
    NSArray             *monArr;
    NSMutableArray      *daArr;
    NSString            *yeaStr;
    NSString            *montStr;
    NSString            *daStr;
    UIView              *withShowPicView;

}
-(instancetype)init{

    self = [super init];
    
    if (self) {
        
        yeaArr = [[NSMutableArray alloc]init];

        UIView *view = [self createUI];
        
        [self currDate];
        
        [self addSubview:view];
    }
    
    return self;
}


-(void)currDate{
    NSDate *now = [NSDate date];
    NSLog(@"now date is: %@", now);
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    
    int year = (int)[dateComponent year];
    int month = (int)[dateComponent month];
    int day = (int)[dateComponent day];
    int hour = (int)[dateComponent hour];
    int minute = (int)[dateComponent minute];
    int second = (int)[dateComponent second];
    
    yeaStr = [NSString stringWithFormat:@"%i", year];
    montStr = [NSString stringWithFormat:@"%i", month];
    daStr = [NSString stringWithFormat:@"%i", day];
    
    NSLog(@"year is: %d", year);
    NSLog(@"month is: %d", month);
    NSLog(@"day is: %d", day);
    NSLog(@"hour is: %d", hour);
    NSLog(@"minute is: %d", minute);
    NSLog(@"second is: %d", second);
    [self pingRun:year-1 Ok:NO];
    [self pingRun:year Ok:NO];
    [self pingRun:year+1 Ok:YES];
    [_pickerView selectRow:1 inComponent:0 animated:YES];
    [_pickerView selectRow:month-1 inComponent:2 animated:YES];
    [_pickerView selectRow:day-1 inComponent:4 animated:YES];
    
    
}
-(void)pingRun:(int)yea Ok:(BOOL)ok{
    if(((yea%4==0)&&(yea%100!=0))||(yea%400==0)){
        NSLog(@"闰年");
        NSString *cityPath=[[NSBundle mainBundle] pathForResource:@"RunYear" ofType:@"plist"];
        monArr=[NSArray arrayWithContentsOfFile:cityPath];
    }
    else
    {
        NSLog(@"平年");
        NSString *cityPath=[[NSBundle mainBundle] pathForResource:@"PingYear" ofType:@"plist"];
        monArr=[NSArray arrayWithContentsOfFile:cityPath];
    }
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:monArr forKey:[NSString stringWithFormat:@"%i", yea]];
    [yeaArr addObject:dic];
    //    NSLog(@"%@", [NSString stringWithFormat:@"%i", yea]);
    //    NSLog(@"%@", monArr);
    //    NSLog(@"%lu", (unsigned long)monArr.count);
    //    NSLog(@"%lu", (unsigned long)yeaArr.count);
    if (ok) {
        [_pickerView reloadAllComponents];
        
    }
    
    
}


-(UIView *)createUI{

    UIView *baseView = [[UIView alloc]init];
    
    
    withShowPicView = [[UIView alloc]init];
    withShowPicView.hidden = YES;
    [self addSubview:withShowPicView];
    withShowPicView.backgroundColor = [UIColor whiteColor];
    [withShowPicView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.equalTo(self.mas_height);
    }];
    
    
    
    UIImageView *whichView = [[UIImageView alloc]init];
    whichView.image = [UIImage imageNamed:@"whitchB"];
    whichView.userInteractionEnabled = YES;
    [withShowPicView addSubview:whichView];
    [whichView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(withShowPicView.mas_top);
        make.left.equalTo(withShowPicView.mas_left);
        make.right.equalTo(withShowPicView.mas_right);
        make.height.equalTo(@(100/2));
    }];
    
    
    UIButton *cancelButton = [[UIButton alloc]init];
    //    cancelButton.backgroundColor = [UIColor blackColor];
    [whichView addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(whichView.mas_centerY);
        make.left.equalTo(whichView.mas_left).offset(32/2);
        //        make.right.equalTo(withShowPicView.mas_right);
        make.height.equalTo(@(48/2));
    }];
    [cancelButton setImage:[UIImage imageNamed:@"cancelNbUTTON"] forState:UIControlStateNormal];
    [cancelButton setImage:[UIImage imageNamed:@"cancelButton"] forState:UIControlStateSelected];

    UILabel *centerLabel = [[UILabel alloc]init];
    [whichView addSubview:centerLabel];
    [centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cancelButton.mas_centerY);
        make.centerX.equalTo(whichView.mas_centerX);
        //        make.right.equalTo(withShowPicView.mas_right);
        //        make.height.equalTo(@(48/2));
    }];
    
    
    centerLabel.font = [UIFont systemFontOfSize:32/2];
    centerLabel.text = @"选择时间";
    centerLabel.textColor = [UIColor colorWithRed:0x74/255.0 green:0x75/255.0 blue:0x79/255.0 alpha:1];
    UIButton *okButton = [[UIButton alloc]init];
    [whichView addSubview:okButton];
    [okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cancelButton.mas_centerY);
        make.right.equalTo(whichView.mas_right).offset(-32/2);
        //        make.right.equalTo(withShowPicView.mas_right);
        make.height.equalTo(@(48/2));
    }];
    
    UIButton *resetButton = [[UIButton alloc]init];
    //    cancelButton.backgroundColor = [UIColor blackColor];
    [whichView addSubview:resetButton];
    [resetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(whichView.mas_centerY);
        make.left.equalTo(centerLabel.mas_right).offset(5);
        make.right.equalTo(okButton.mas_left);
        make.height.equalTo(@(48/2));
    }];

    [resetButton addTarget:self action:@selector(resetClick) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [okButton addTarget:self action:@selector(okClick) forControlEvents:UIControlEventTouchUpInside];
    
    [resetButton setImage:[UIImage imageNamed:@"cancelNbUTTON"] forState:UIControlStateNormal];
    [resetButton setImage:[UIImage imageNamed:@"cancelButton"] forState:UIControlStateSelected];
    [okButton setImage:[UIImage imageNamed:@"okNButton"] forState:UIControlStateNormal];
    [okButton setImage:[UIImage imageNamed:@"okButotn"] forState:UIControlStateSelected];
    whichView.backgroundColor = [UIColor grayColor];
    //    cancelButton.backgroundColor = [UIColor blackColor];
    //    okButton.backgroundColor = [UIColor orangeColor];
    
    

    _pickerView = [[UIPickerView alloc]init];
    _pickerView.backgroundColor = [UIColor whiteColor];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    _pickerView.showsSelectionIndicator = YES;
    
    //    [_pickerView selectRow:1 inComponent:0 animated:YES];
    //    [_pickerView selectRow:month-1 inComponent:2 animated:YES];
    //    [_pickerView selectRow:day-1 inComponent:4 animated:YES];
    _pickerView.hidden = NO;
    [withShowPicView addSubview:_pickerView];
    [_pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(withShowPicView.mas_centerX);
        if(CURRENTVERSION<9.0&&CURRENTVERSION>=8.0){
            
        make.bottom.equalTo(withShowPicView.mas_bottom);
        make.top.equalTo(whichView.mas_bottom).offset(50);
        }else{
            
                    make.bottom.equalTo(withShowPicView.mas_bottom);
                    make.top.equalTo(whichView.mas_bottom);
            

        }
        make.width.equalTo(withShowPicView.mas_width);
    }];

    return baseView;
}

#pragma mark pickerViewDelegate
//返回组数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerVie{
    return 6;
}
//返回行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSLog(@"%li", (long)component);
    if(yeaArr.count<3)return 0;
    //    return ((NSArray *)yeaArr[component]).count;
    if(component == 0){
        return yeaArr.count;
    }else if(component == 1){
        return 1;
        
    }else if (component == 2){
        return 12;
    }else if (component == 3){
        return 1;
    } else if(component == 4){
        //        NSLog(@"%@", yeaArr);
        NSInteger row1=[_pickerView selectedRowInComponent:0];
        NSInteger row2=[_pickerView selectedRowInComponent:2];
        //然后是获取这个行中的值，就是数组中的值
        //        int a = [label.text intValue];
        
        //        int b = [label1.text intValue];
        
        NSDictionary *dic = yeaArr[row1];
        NSArray *arr2 = [dic allKeys];
        NSArray *arr = [dic valueForKey:arr2[0]];
        NSArray *arr1 = arr[row2];
        //        NSLog(@"%@", arr);
        //        NSLog(@"%@", arr1);
        return arr1.count;
    }else{
        return 1;
    }
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    return @"2015";
}
//选中row
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    UILabel *label = (UILabel *)[pickerView viewForRow:row forComponent:component];
    NSLog(@"%@", label.text);
    [self textData:label.text andCompoment:component];
    if (component == 2) {
        [_pickerView reloadComponent:4];
    }
    NSLog(@"row:%ld",(long)row);
}

-(void)textData:(NSString *)text andCompoment:(NSInteger)compoment{
    NSLog(@"%@", text);
    if (compoment == 0) {
        yeaStr = text;
    }else if(compoment == 2){
        montStr = text;
    }else if (compoment == 4){
        daStr = text;
    }
    [self testString];
}

-(void)testString{
    if (yeaStr&&montStr&&daStr) {
        NSLog(@"%@", [NSString stringWithFormat:@"%@-%@-%@", yeaStr, montStr, daStr]);
        //        _pickerView.hidden = YES;
        //        withShowPicView.hidden = YES;
        //        yeaStr = nil;
        //        montStr = nil;
        //        daStr = nil;
    }
}
//设置选取器的字体
- (UILabel *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(50, 10, 150, 50)];
    if(component == 0){
        //        NSArray *arr = [(NSDictionary *)(yeaArr[component]) allKeys];
        ////        NSArray *arr = [(NSDictionary *)(yeaArr[component]) allKeys];
        ////        NSArray *arr = [(NSDictionary *)(yeaArr[component]) allKeys];
        //        NSLog(@"%@", yeaArr);
        //        NSLog(@"%@", arr);
        if(row == 0){
            NSArray *arr = [(NSDictionary *)(yeaArr[component]) allKeys];
            label.text=arr[0];
        }else if(row == 1){
            NSArray *arr = [(NSDictionary *)(yeaArr[1]) allKeys];
            label.text=arr[0];
        }else if(row == 2){
            NSArray *arr = [(NSDictionary *)(yeaArr[2]) allKeys];
            label.text=arr[0];
        }
    }else if (component == 1){
        label.text = @"年";
    } else if(component == 2){
        //        NSArray *arr = [(NSDictionary *)(yeaArr[component]) allKeys];
        label.text=[NSString stringWithFormat:@"%i", (int)row+1];
        
    }else if (component == 3){
        label.text = @"月";
    } else if(component == 4){
        //        NSLog(@"%@", yeaArr);
        NSInteger row1=[_pickerView selectedRowInComponent:0];
        NSInteger row2=[_pickerView selectedRowInComponent:1];
        //然后是获取这个行中的值，就是数组中的值
        //        UILabel *label1 = (UILabel *)[pickerView  viewForRow:row1 forComponent:0];
        //        int a = [label.text intValue];
        
        //        UILabel *label2 = (UILabel *)[pickerView  viewForRow:row2 forComponent:1];
        //        int b = [label1.text intValue];
        
        NSDictionary *dic = yeaArr[row1];
        NSArray      *arr2 = [dic allKeys];
        NSArray *arr = [dic valueForKey:arr2[0]];
        NSArray *arr1 = arr[row2];
//        NSLog(@"%@", arr);
//        NSLog(@"%@", arr1);
        label.text = arr1[row];
        
        //        label.text = [NSString stringWithFormat:@"%i", (int)row+1];
    }else if (component == 5){
        label.text = @"日";
    }
    
    //    label.text=@"2015";
    label.textAlignment=NSTextAlignmentCenter;
    label.font=[UIFont systemFontOfSize:25];
    return label;
}


#pragma mark - show pickerView and the cancel btn and the sure btn
-(void)cancelClick{
    withShowPicView.hidden = YES;
    
    if (self.delegateView) {
        [self.delegateView performSelector:@selector(rightBtnClick1:) withObject:@""];
    }

}


-(void)resetClick{
    if (self.delegateView) {
        [self.delegateView performSelector:@selector(rightBtnClick1:) withObject:@""];
    }
    
}

-(void)okClick{
    
    /**
     yeaStr, montStr, daStr 即为年月日
     */
    if (yeaStr&&montStr&&daStr) {
//        _pickerView.hidden = YES;
        withShowPicView.hidden = YES;
                NSString *str = [NSString stringWithFormat:@"%@-%@-%@", yeaStr, montStr, daStr];
//                    yeaStr = nil;
//                    montStr = nil;
//                    daStr = nil;
//        CustomTextField *textField = (CustomTextField *)[self.view viewWithTag:1000+_rowsNum];
//        textField.hidden = NO;
//        textField.text = [NSString stringWithFormat:@"%@-%@-%@", yeaStr, montStr, daStr];
//        CustomTextField *titleText = (CustomTextField *)[self.view viewWithTag:1000+_rowsNum];
//        titleText.hidden = NO;
//        NSString *str =[NSString stringWithFormat:@"%@-%@-%@", yeaStr, montStr, daStr];
//        titleText.text = str;
//        NSDictionary *dic= [_fieldArr objectAtIndex:_rowsNum];
//        NSString *fieldRole= [dic objectForKey:@"fieldRole"];
//        NSString *fieldId= [dic objectForKey:@"fieldId"];
//        NSString *pageId= [_EditDic objectForKey:@"startTurnPage"];
//        NSString *tableId= [_dataDic objectForKey:@"tableId"];
//        NSString *baseString = [NSString stringWithFormat:@"_au_%@_%@_%@",tableId,pageId,fieldId];
//        [_sendMutDic setObject:str forKey:[KeyNameClass getKeyName:fieldRole andBaseString:baseString]];
//        NSLog(@"%@",_sendMutDic);
        if (self.delegateView) {
            [self.delegateView performSelector:@selector(rightBtnClick1:) withObject:str];
        }
    }
}


-(void)startAnima{
//    timeSelectView.hidden = NO;
    withShowPicView.hidden = NO;

}

-(void)endAnima{
    withShowPicView.hidden = YES;

}

@end
